<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
       <!-- vue引入 -->
       <script src="./vue.min.js"></script>
       <!-- 引入vue router -->
       <script src="./vue-router.min.js"></script>
</head>

<body>
    <!-- 被 vm 实例所控制的区域 -->
    <div id="app">
        <router-link to="/user/1">User1</router-link>
        <router-link to="/user/2">User2</router-link>
        <router-link :to="{ name: 'user', params: {id: 3} }">User3</router-link>
        <router-link to="/register">Register</router-link>

        <!-- 路由占位符 -->
        <router-view></router-view>
    </div>

    <script>
        const User = {
            props: ['id', 'uname', 'age'],
            template: `<div>
          <h1>User 组件 -- 用户id为: {{id}} -- 姓名为:{{uname}} -- 年龄为：{{age}}</h1>
          <button @click="goRegister">跳转到注册页面</button>
        </div>`,
            methods: {
                goRegister() {
                    // $router路由器  提供了一系列的方法 让我们来管理路由，实现各种路由功能
                    // this.$router.push('/register')

                    // this.$router.push({name: 'register', query: {id: 2}})

                    // this.$router.push({path: '/register'})


                    // push方法 和 replace方法
                    //  replace替换
                    this.$router.replace({name: 'register'})




                    // console.log(this.$router)
                    // console.log(this.$route)
                    // this.$router.push('/register');
                    // this.$router.push({
                    //     name: 'register'
                    // })

                    // this.$router.replace({
                    //     name: 'register'
                    // })
                }
            },
            created() {
                console.log(this.$route)
                console.log(this.$router)
            }
        }

        const Register = {
            template: `<div>
          <h1>Register 组件</h1>
          <button @click="goBack">后退</button>
        </div>`,
            methods: {
                goBack() {
                    this.$router.go(-1)
                }
            }
        }

        // 创建路由实例对象
        const r = new VueRouter({
            // 所有的路由规则
            routes: [{
                path: '/',
                redirect: '/user'
            }, {
                // 命名路由
                name: 'user',
                path: '/user/:id',
                component: User,
                props: route => ({
                    uname: 'zs',
                    age: 20,
                    id: route.params.id
                })
            }, {
                path: '/register',
                component: Register,
                name: 'register'
            }]
        })

        // 创建 vm 实例对象
        const vm = new Vue({
            // 指定控制的区域
            el: '#app',
            data: {},
            // 挂载路由实例对象
            // router: router
            router:r
        })
    </script>
</body>

</html>
